<?php
/**
 * CpController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class UploadController extends Thaind_Controller_Action
{
	var $_arrParam;
	public function init()
    {
//    	Truyền các tham số nhận được ra view
    	$this->_arrParam = $this->_request->getParams();
    	$this->view->arrParam = $this->_arrParam;
    }

    public function preDispatch(){
  
        $auth = Thaind_Auth_User::getInstance();
        if (!$auth->hasIdentity()) {
    		$this->_response->clearBody();
			$this->_response->clearHeaders();
			$this->_response->setHttpResponseCode(401);
			exit();
    	}
		$this->_helper->viewRenderer->setNoRender();
    	$this->_helper->layout->disableLayout();
    }
    /**
     * Action upload file
     */
    public function indexAction ()
    {
    	$upload = new Zend_File_Transfer();
 
		$upload->setDestination(PUBLIC_PATH . $this->_arrParam['folder']);
		// Returns all known internal file information
		$file_info = $upload->getFileInfo();
		
		$upload->receive();
		
		$confirm_code = uniqid(FALSE);
		                
		/**
		* file_name : Tên của file gốc truyền lên
		*/
		                
		$file_name = explode(".", $this->CovertUnicodeString($file_info['file']['name']));
		                
		/**
		* ext : extension của file truyền lên 
		*/
		                
		$ext = end($file_name);
		array_pop($file_name);
		                
		/**
		* fullPathNameFile : Đường dẫn tương đối tới file sau khi up lên 
		*/
		                
		$fullPathNameFile = $this->_arrParam['folder'] . "/" . implode("", $file_name) . $confirm_code . "." . $ext;	                
		
		/**
		* Copy file từ thư mục chứa file tạm sang thư mục mình định nghĩa
		*/             

		$filterRename = new Zend_Filter_File_Rename(array('target' => PUBLIC_PATH . $fullPathNameFile, 'overwrite' => true)); 
		$filterRename->filter(PUBLIC_PATH . $this->_arrParam['folder'] . "/" . $file_info['file']['name']);
		
		echo $fullPathNameFile;
    }
	/**
     * Hàm chuyển chuỗi utf-8 thành chuỗi không dấu
     */
    
	function CovertUnicodeString($str) {
			$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
			$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
			$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
			$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
			$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
			$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
			$str = preg_replace("/(đ)/", 'd', $str);
			$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
			$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
			$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
			$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
			$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
			$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
			$str = preg_replace("/(Đ)/", 'D', $str);
			$str = preg_replace("/( )/", '_', $str);
			$str = preg_replace("/(_)/", '_', $str);
			//$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
			return $str;
   	}
}
